﻿/**
 * 辅助方法注入
 */
(function () {
    /**
     * 时间转字符串
     * @param {any} format 时间格式
     */
    Date.prototype.format = function (format) {
        if (format == undefined) {
            //console.log(format);
            format = "yyyy-MM-dd hh:mm:ss";
        }

        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                    date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    };
    /**
     * 初始化一个时间对象
     * @param {Number?} y
     * @param {Number?} M
     * @param {Number?} d
     * @param {Number?} h
     * @param {Number?} m
     * @param {Number?} s
     * @param {Number?} S
     */
    Date.prototype.Load = function (y, M, d, h, m, s, S) {
        if (y == undefined) {
            y = 1970;
        }
        if (M == undefined) {
            M = 0;
        }
        if (d == undefined) {
            d = 1;
        }
        if (h == undefined) {
            h = 0;
        }
        if (m == undefined) {
            m = 0;
        }
        if (s == undefined) {
            s = 0;
        }
        if (S == undefined) {
            S = 0;
        }
        this.setFullYear(y);
        this.setMonth(M - 1);
        this.setDate(d);
        this.setHours(h);
        this.setMinutes(m);
        this.setSeconds(s);
        this.setMilliseconds(S);
    }

    Date.prototype.AddDay = function (days) {
        this.setDate(this.getDate() + days);
        return this;
    }

    var helper = {};
    if (window.Helper == undefined) {
        window.Helper = helper;
    }
    else {
        helper = window.Helper;
    }
}());
(function () {
    var helper = {};
    if (window.Helper.Date == undefined) {
        window.Helper.Date = helper;
    }
    else {
        helper = window.Helper.Date;
    }
    var that = helper;
    /**
     * 获取周的范围
     * @param {Date|String} date 月份
     * @param {Number|String} weekIndex 周
     */
    helper.GetWeekSize = function (date, weekIndex) {
        //判断周期类型
        if (typeof (date) == "string") {
            var arry = data.split('-');
            var _date = new Date(parseInt(arry[0]), parseInt(arry[1]) - 1);
            date = _date;
        }
        var arry = this.GetWeekArry(date);
        //console.log(arry);
        if (weekIndex > arry.length) {
            return null;
        }

        //console.log(arry);
        return arry[weekIndex - 1];
    };
    helper.GetWeekFirs = function (date) {
        var _date = new Date(date.getFullYear(), date.getMonth(), 1);
        var date1 = new Date(2019, 0, 21);
        //console.log(_date.format());
        //console.log(date1.getDay());
        //console.log("当前第一周的星期" + (_date.getDay() == 0 ? "天" : (_date.getDay()-1)));

        return _date.getDay();
    };
    helper.GetMonthMaxDay = function (date) {
        var _date = new Date(date.getFullYear(), (date.getMonth() + 1), 0);
        //console.log("最后一天时间：" + _date.format());
        return parseInt(_date.format("dd"));
    }
    helper.GetWeekArry = function (date) {
        //获取当前月的第一天是周几
        var firstCyc = this.GetWeekFirs(date);
        if (firstCyc == 0) {
            firstCyc = 7;
        }
        firstCyc -= 1;
        //console.log(firstCyc);
        //console.log(date.format());
        //获取当月的最大天数
        var maxDay = this.GetMonthMaxDay(date);
        var currentDay = 0;
        var arry = [];
        var weekIndex = 0;
        do {
            var startDay, endDay;
            startDay = currentDay + 1;
            if (currentDay == 0) {
                endDay = currentDay + (7 - (firstCyc));
                currentDay = endDay;
            }
            else {
                if (currentDay + 7 >= maxDay) {
                    endDay = maxDay;
                    currentDay = null;
                }
                else {
                    endDay = currentDay + 7;
                    currentDay = endDay;
                }
            }
            weekIndex++;
            var obj = {};
            obj.Index = weekIndex;
            obj.StartDay = startDay;
            obj.StartDate = new Date(date.getFullYear(), date.getMonth(), startDay);
            obj.EndDay = endDay;
            obj.EndDate = new Date(date.getFullYear(), date.getMonth(), endDay);
            arry.push(obj);
        } while (currentDay != null);

        var _arry = [];
        for (var i = 0; i < arry.length; i++) {
            var current = arry[i];
            if (i == 1) {
                if ((_arry[0].EndDay - _arry[0].StartDay) <= 4) {
                    _arry[0].EndDate = current.EndDate;
                }
                else {
                    _arry.push(current);
                }
            }
            else if (i == arry.length - 1) {
                if ((current.EndDay - current.StartDay) <= 4) {
                    _arry[_arry.length - 1].EndDate = current.EndDate;
                }
                else {
                    _arry.push(current);
                }
            }
            else {
                _arry.push(current);
            }
        }
        //console.log(maxDay);
        for (var i = 0; i < _arry.length; i++) {
            _arry[i].Index = i + 1;
            _arry[i].StartDay = _arry[i].StartDate.getDay() + 1;
            _arry[i].EndDay = _arry[i].EndDate.getDay() + 1;
        }
        console.log(_arry);
        return _arry;
    }
    helper.GetWeekSizeByDate = function (date) {
        var arry = this.GetWeekArry(date);
        for (var i = 0; i < arry.length; i++) {
            if (arry[i].StartDate <= date && arry[i].EndDate >= date) {
                return arry[i];
            }
        }
    }
    Date.prototype.GetMonthMaxDay = function () {
        return that.GetMonthMaxDay(this);
    }
}());
(function () {
    var helper = {};
    if (window.Helper.Float == undefined) {
        window.Helper.Float = helper;
    }
    else {
        helper = window.Helper.Float;
    }
    var that = helper;
    //console.log(that);
    //解决js运算精度丢失问题  加法
    that.add = function (arg1, arg2) {
        try {
            var r1, r2, m;
            try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
            try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
            m = Math.pow(10, Math.max(r1, r2))
            return (arg1 * m + arg2 * m) / m;
        } catch (e) {
            return 0;
        }
    }
    //解决js运算精度丢失问题  减法
    that.cut = function (arg1, arg2) {
        try {
            var r1, r2, m, n;
            try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
            try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
            m = Math.pow(10, Math.max(r1, r2));
            //last modify by deeka
            //动态控制精度长度
            n = (r1 >= r2) ? r1 : r2;
            return ((arg1 * m - arg2 * m) / m).toFixed(n);
        } catch (e) {
            return 0;
        }
    }
    //解决js运算精度丢失问题  乘法
    that.mul = function (arg1, arg2) {
        try {
            var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
            try { m += s1.split(".")[1].length } catch (e) { }
            try { m += s2.split(".")[1].length } catch (e) { }
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
        } catch (e) {
            return 0;
        }
    }
    //解决js运算精度丢失问题  除法
    that.div = function (arg1, arg2) {
        try {
            var t1 = 0, t2 = 0, r1, r2;
            try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
            try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
            with (Math) {
                r1 = Number(arg1.toString().replace(".", ""))
                r2 = Number(arg2.toString().replace(".", ""))
                return (r1 / r2) * pow(10, t2 - t1);
            }
        } catch (e) {
            return 0;
        }
    }
    //转换为千叶符
    that.QyFormat = function (arg1) {
        /*
        var num = arg1;
        num = num.toString().split(".");  // 分隔小数点
        var arr = num[0].split("").reverse();  // 转换成字符数组并且倒序排列
        var res = [];
        for (var i = 0, len = arr.length; i < len; i++) {
            if (i % 3 === 0 && i !== 0) {
                res.push(",");   // 添加分隔符
            }
            res.push(arr[i]);
        }
        res.reverse(); // 再次倒序成为正确的顺序
        if (num[1]) {  // 如果有小数的话添加小数部分
            res = res.join("").concat("." + num[1]);
        } else {
            res = res.join("");
        }
        return res;
        */
        if (arg1 == undefined || arg1 == null || arg1 == "") {
            return "";
        }

        var res = arg1.toString().replace(/\d+/, function (n) { // 先提取整数部分
            return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
                return $1 + ",";
            });
        })
        return res;
    }
}());
(function () {
    var helper = {};
    if (window.Helper.Url == undefined) {
        window.Helper.Url = helper;
    }
    else {
        helper = window.Helper.Url;
    }
    var that = helper;
    //参数转化为Url参数
    that.ParmsToString = function (parms) {
        var url = "";
        if (Array.isArray(parms)) {
            for (var i = 0; i < parms.length; i++) {
                var item = parms[i];
                if (i == 0) {
                    url += "?";
                }
                else {
                    url += "&";
                }
                url += that.Encode(item.Key) + "=" + that.Encode(item.Value);
            }
        }
        else {
            var arry = [];
            for (var i in parms) {
                arry.push({ Key: i, Value: parms[i].toString() });
            }
            return that.ParmsToString(arry);
        }
        return url;
    };
    //URL编码
    that.Encode = function (str) {
        return window.encodeURIComponent(str);
    };
    //URL解码
    that.DeEncode = function (str) {
        console.log("转码前：" + str);
        console.log("转码后：" + window.decodeURIComponent(str));
        return window.decodeURIComponent(str, true);
    };
    that.Query = function (name, ifCode) {
        if (ifCode == undefined) {
            ifCode = true;
        }
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);

        if (r != null) {
            if (ifCode) {
                return window.Helper.Url.DeEncode(r[2]);
            }
            else {
                return r[2];
            }
        }
        return null;
    }
}());
(function () {
    var helper = {};
    if (window.Helper.Object == undefined) {
        window.Helper.Object = helper;
    }
    else {
        helper = window.Helper.Object;
    }
    var that = helper;
    //浅复制一个对象
    that.Copy = function (obj) {
        var _obj = {};
        for (var i in obj) {
            _obj[i] = obj[i];
        }
        return _obj;
    };
}());